package com.heaven.customerServer.customerController;

import com.heaven.constant.CommentConstants;
import com.heaven.core.entity.CommentEntity;
import com.heaven.customerServer.customerController.vo.comment.LoadCommentRes;
import com.heaven.customerServer.customerController.vo.comment.SendCommentReq;
import com.heaven.service.CommentService;
import com.heaven.utils.R;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("heaven-server/comment")
public class CommentController {
    @Resource
    private CommentService commentService;
    @Resource
    private LoginController loginController;

    /**
     * 发送评论
     */
    @PostMapping("/auth/send")
    public R sendComment(@Valid @RequestBody SendCommentReq req) {
        CommentEntity commentEntity = commentService.addComment(req, loginController.getUserSession().getUserId());
        Map<String, Object> data = new HashMap<>();
        data.put("commentId", commentEntity.getCommentId());
        if(!req.getFatherId().equals(req.getToCommentId())) {
            CommentEntity toComment = commentService.selectById(req.getToCommentId());
            data.put("toUserInfo", commentService.getCommentUserInfo(toComment.getUserId()));
        }
        return R.okData(data);
    }

    /**
     * 加载评论
     * @param targetId 媒体对象ID（视频、文贴）
     * @param type 评论归属媒体的类型 video:1, article:2
     * @param sort 排序方式 hot:热度, time:时间
     * @param page 页码
     * @param size 大小
     * @return 评论列表
     */
    @GetMapping("/load")
    public R loadComment(@RequestParam("targetId") String targetId,
                         @RequestParam("type") int type,
                         @RequestParam("sort") String sort,
                         @RequestParam("page") int page,
                         @RequestParam("size") int size) {
        if(type != CommentConstants.COMMENT_TYPE_VIDEO && type != CommentConstants.COMMENT_TYPE_ARTICLE) {
            return R.error("type error");
        }
        if(!sort.equals(CommentConstants.COMMENT_SORT_HOT) && !sort.equals(CommentConstants.COMMENT_SORT_TIME)) {
            return R.error("sort error");
        }
        List<LoadCommentRes> ans = commentService.loadComment(targetId, type, page, size, sort);
        return R.okData(ans);
    }

    /**
     * 处理评论的点赞和踩
     * @param commentId 评论ID
     * @param type 1:点赞键,2:踩键
     */
    @RequestMapping("/auth/like-dislike")
    public R handleLikeOrDisLike(@RequestParam("commentId") String commentId, @RequestParam("type") int type) {
        if(type != CommentConstants.COMMENT_HANDLE_LIKE && type != CommentConstants.COMMENT_HANDLE_DISLIKE) {
            return R.error("type error");
        }
        String userId = loginController.getUserSession().getUserId();
        commentService.handleLikeOrDislike(commentId, userId, type);
        return R.ok();
    }
}
